<template>
	<sc-dialog v-model="visible" :title="titleMap[mode]" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="160px" :model="formData" :rules="rules">
			<el-form-item label="工艺路线" prop="routingCode">
				<el-input v-model="formData.routingCode" disabled> </el-input>
			</el-form-item>
			<el-form-item label="工序" prop="processId">
				<sc-table-select
					v-model="opSelectValue"
					placeholder="请选择工序"
					:apiObj="apiObj"
					:params="reqParams"
					:props="{
						label: 'procCode',
						value: 'id',
					}"
					@change="change">
					<el-table-column prop="procCode" label="工序编号" width="120" />
					<el-table-column prop="procName" label="工序名称" width="220" />
				</sc-table-select>
			</el-form-item>
			<el-form-item label="标准加工工时(秒)" prop="stdOpDuration">
				<el-input-number v-model="formData.stdOpDuration" :precision="1" :min="0" :max="86400" />
			</el-form-item>
			<el-form-item label="标准准备工时(秒)" prop="stdPrepDuration">
				<el-input-number v-model="formData.stdPrepDuration" :precision="1" :min="0" :max="86400" />
			</el-form-item>
			<el-form-item label="校验上一工序" prop="policyCheckPrevProc">
				<el-radio-group v-model="formData.policyCheckPrevProc">
					<el-radio
						v-for="item in policyCheckPrevProcOptions"
						:key="item.value"
						:label="item.value"
						>{{ item.label }}</el-radio
					>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="校验生产BOM" prop="policyCheckProcBom">
				<el-radio-group v-model="formData.policyCheckProcBom">
					<el-radio
						v-for="item in policyCheckProcBomOptions"
						:key="item.value"
						:label="item.value"
						>{{ item.label }}</el-radio
					>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="工艺校验策略" prop="policyCheckProcess">
				<el-checkbox-group v-model="policyCheckProcessChecked">
					<el-checkbox v-for="item in policyCheckProcessOptions" :key="item.value" :label="item.value">
						{{ item.label }}
					</el-checkbox>
				</el-checkbox-group>
			</el-form-item>
			<el-form-item label="工序管控" prop="ctlAttr">
				<el-radio-group v-model="formData.ctlAttr">
					<el-radio
						v-for="item in ctlAttrOptions"
						:key="item.value"
						:label="item.value"
						>{{ item.label }}</el-radio
					>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="是否禁用" prop="isDisabled">
				<el-switch v-model="formData.isDisabled" />
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
export default {
	emits: ['complete'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			apiObj: this.$API.procprocess.pagesbylineid,
			reqParams: {
				routingId: 0,
			},
			opSelectValue: {},
			policyCheckPrevProcOptions: [],
			policyCheckProcBomOptions: [],
			policyCheckProcessOptions: [],
			policyCheckProcessChecked: [],
			ctlAttrOptions: [],
			formData: {
				id: 0,
				routingId: undefined,
				routingCode: '',
				processId: undefined,
				procCode: '',
				stdOpDuration: 0.0,
				stdPrepDuration: 0.0,
				pos: 1,
				policyCheckPrevProc: 0,
				policyCheckProcBom: 1,
				policyCheckProcess: 0,
				ctlAttr: 1,
				isDisabled: false,
			},
			rules: {
				routingId: [{ required: true, message: "工艺路线不能为空", trigger: "blur" }],
				processId: [{ required: true, message: "请选择工序", trigger: "blur" }],
				stdOpDuration: [{ required: true, message: "请输入标准加工工时(秒)", trigger: "blur" }],
				stdPrepDuration: [{ required: true, message: "请输入标准准备工时(秒)", trigger: "blur" }],
				policyCheckPrevProc: [{ required: true, message: "请输入校验上一工序动作", trigger: "blur" }],
				policyCheckProcBom: [{ required: true, message: "请选择校验生产BOM项", trigger: "blur" }],
				ctlAttr: [{ required: true, message: "请选择工序管控项", trigger: "blur" }],
			},
		};
	},
	methods: {
		change(item) {
			this.formData.processId = item.id;
			this.formData.procCode = item.procCode;
		},
		async open(row, param) {
			Object.assign(this.formData, param);
			Object.assign(this.reqParams, param);

			if (!row) {
				this.mode = "add";
				const resp0 = await this.$API.procroutingitem.countbyroutingid.get(this.formData.routingId);
				this.formData.pos = resp0.data + 1;
			} else {
				this.mode = "edit";
				let resp = await this.$API.procroutingitem.model.get(row.id);
				this.formData = resp.data;

				this.opSelectValue = { id: this.formData.processId, procCode: this.formData.procCode };
				this.policyCheckProcessChecked = resp.data.policyCheckProcessChecked.map(s => s.value);
				console.log('data', resp.data);
			}

			const resp2 = await this.$API.enumlist.model.get('policyCheckPrevProcEnum');
			this.policyCheckPrevProcOptions = resp2.data;
			const resp3 = await this.$API.enumlist.model.get('policyCheckProcBomEnum');
			this.policyCheckProcBomOptions = resp3.data;
			const resp4 = await this.$API.enumlist.model.get('policyCheckProcessEnum');
			this.policyCheckProcessOptions = resp4.data;
			const resp5 = await this.$API.enumlist.model.get('procCtlAttrEnum');
			this.ctlAttrOptions = resp5.data;

			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;

					// Flags 选项合并处理
					let checked = 0;
					this.policyCheckProcessChecked.forEach(item => {
						checked = checked | item;
					});
					this.formData.policyCheckProcess = checked;

					let resp = null;
					if (this.formData.id === 0) {
						resp = await this.$API.procroutingitem.add.post(this.formData);
					} else {
						resp = await this.$API.procroutingitem.update.put(this.formData);
					}
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.opSelectValue = {};
			this.formData = {
				id: 0,
				routingId: undefined,
				routingCode: '',
				processId: undefined,
				procCode: '',
				stdOpDuration: 0.0,
				stdPrepDuration: 0.0,
				pos: 1,
				policyCheckPrevProc: 0,
				policyCheckProcBom: 1,
				policyCheckProcess: 0,
				ctlAttr: 1,
				isDisabled: false,
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
